package com.j2eefast.flowable.bpm.controller;

import com.j2eefast.common.core.base.entity.LoginUserEntity;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.*;
import com.j2eefast.flowable.bpm.entity.BpmTaskFromEntity;
import com.j2eefast.flowable.bpm.enums.CommentOSSType;
import com.j2eefast.flowable.bpm.service.BpmTaskFromService;
import com.j2eefast.flowable.bpm.utils.CommontOSSUtils;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.system.oss.entity.SysOssEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.ui.ModelMap;

import java.util.List;
import java.util.Map;
import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.j2eefast.flowable.bpm.entity.bpmCrmRemittanceEntity;
import com.j2eefast.flowable.bpm.service.bpmCrmRemittanceService;

/**
 * 汇款信息页面控制器
 * @author yhli
 * @date 2020-06-28 14:12
 */
@Controller
@RequestMapping("/bpm/remittance")
public class bpmCrmRemittanceController extends BaseController{

    private String prefix = "modules/bpm/remittance";
    @Autowired
    @Lazy(value = true)
    private CommontOSSUtils commontOSSUtils;
    @Autowired
    private bpmCrmRemittanceService bpmCrmRemittanceService;
    /**
    * 必须注入实例关联表单服务
    */
    @Autowired
    private BpmTaskFromService bpmTaskFromService;


    @RequiresPermissions("bpm:remittance:view")
    @GetMapping()
    public String remittance(){
        return prefix + "/remittance";
    }
        
    @RequestMapping("/list")
    @RequiresPermissions("bpm:remittance:list")
    @ResponseBody
    public ResponseData list(@RequestParam Map<String, Object> params,bpmCrmRemittanceEntity bpmCrmRemittanceEntity) {
		PageUtil page = bpmCrmRemittanceService.findPage(params,bpmCrmRemittanceEntity);
		return success(page);
    }    
            
    @GetMapping("/add")
    public String add(ModelMap mmap){
         bpmCrmRemittanceEntity bpmCrmRemittance = new bpmCrmRemittanceEntity();
          mmap.put("bpmCrmRemittance", bpmCrmRemittance);
        LoginUserEntity loginInfo=UserUtils.getUserInfo();
        mmap.put("user", loginInfo);
        String saleOrderNumber=OrderCodeFactory.generateCRMSKAgain(loginInfo.getId());
        mmap.put("saleOrderNumber", saleOrderNumber);
        return prefix + "/add";
    }
        /**
         * 定义关联表单申请表单URL对应此处
         */
        @GetMapping("/add/{id}")
        public String add(@PathVariable("id") Long id, ModelMap mmp){
            //通过页面传入的表单ID查询表单关联信息
            BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getTaskFromById(id);
            mmp.put("bpmTaskFrom", bpmTaskFrom);
            mmp.put("user", UserUtils.getUserInfo());
            return prefix + "/add";
        }

    /**
     * 新增
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:remittance:add")
    @BussinessLog(title = "汇款信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addbpmCrmRemittance(@Validated bpmCrmRemittanceEntity bpmCrmRemittance){

        return bpmCrmRemittanceService.savebpmCrmRemittance(bpmCrmRemittance)? success(): error("新增失败!");
    }    
    
    /**
     * 修改
     */
    @RequiresPermissions("bpm:remittance:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmRemittanceEntity bpmCrmRemittance = bpmCrmRemittanceService.getbpmCrmRemittanceById(id);
        mmap.put("bpmCrmRemittance", bpmCrmRemittance);
        return prefix + "/edit";
    }
    /**
     * 审批页面1
     */
    @RequiresPermissions("bpm:remittance:view1")
    @GetMapping("/view1/{id}")
    public String view1(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmRemittanceEntity bpmCrmRemittance = bpmCrmRemittanceService.getbpmCrmRemittanceById(id);
        mmap.put("bpmCrmRemittance", bpmCrmRemittance);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.CRMRemittanceProof.getName(),bpmCrmRemittance.getSaleOrderNumber());
        mmap.put("proof",proof);
        List<SysOssEntity> proofList = commontOSSUtils.selectListByType(CommentOSSType.CRMRemittancePayProof.getName(),bpmCrmRemittance.getSaleOrderNumber());
        mmap.put("proofList",proofList);
        return prefix + "/view1";
    }

    /**
     * 审批页面2
     */
    @RequiresPermissions("bpm:remittance:view2")
    @GetMapping("/view2/{id}")
    public String view2(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmRemittanceEntity bpmCrmRemittance = bpmCrmRemittanceService.getbpmCrmRemittanceById(id);
        mmap.put("bpmCrmRemittance", bpmCrmRemittance);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.CRMRemittanceProof.getName(),bpmCrmRemittance.getSaleOrderNumber());
        mmap.put("proof",proof);
        List<SysOssEntity> proofList = commontOSSUtils.selectListByType(CommentOSSType.CRMRemittancePayProof.getName(),bpmCrmRemittance.getSaleOrderNumber());
        mmap.put("proofList",proofList);
        return prefix + "/view2";
    }

    /**
     * 审批页面3
     */
    @RequiresPermissions("bpm:remittance:view3")
    @GetMapping("/view3/{id}")
    public String view3(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmRemittanceEntity bpmCrmRemittance = bpmCrmRemittanceService.getbpmCrmRemittanceById(id);
        mmap.put("bpmCrmRemittance", bpmCrmRemittance);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.CRMRemittanceProof.getName(),bpmCrmRemittance.getSaleOrderNumber());
        mmap.put("proof",proof);
        List<SysOssEntity> proofList = commontOSSUtils.selectListByType(CommentOSSType.CRMRemittancePayProof.getName(),bpmCrmRemittance.getSaleOrderNumber());
        mmap.put("proofList",proofList);
        return prefix + "/view3";
    }
    /**
     * 审批页面4
     */
    @RequiresPermissions("bpm:remittance:view4")
    @GetMapping("/view4/{id}")
    public String view4(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmRemittanceEntity bpmCrmRemittance = bpmCrmRemittanceService.getbpmCrmRemittanceById(id);
        mmap.put("bpmCrmRemittance", bpmCrmRemittance);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.CRMRemittanceProof.getName(),bpmCrmRemittance.getSaleOrderNumber());
        mmap.put("proof",proof);
        List<SysOssEntity> proofList = commontOSSUtils.selectListByType(CommentOSSType.CRMRemittancePayProof.getName(),bpmCrmRemittance.getSaleOrderNumber());
        mmap.put("proofList",proofList);
        return prefix + "/view4";
    }

    /**
     * 修改保存汇款信息
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:remittance:edit")
    @BussinessLog(title = "汇款信息", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmCrmRemittance(bpmCrmRemittanceEntity bpmCrmRemittance){
        Long userID=UserUtils.getUserId();
        bpmCrmRemittance.setUpdateBy(userID.toString());
        bpmCrmRemittance.setUpdateTime(new Date());
        return bpmCrmRemittanceService.updatebpmCrmRemittanceById(bpmCrmRemittance)? success(): error("修改失败!");
    }    

    /**
     * 删除
     */
    @RepeatSubmit
    @BussinessLog(title = "汇款信息", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @RequiresPermissions("bpm:remittance:del")
    @ResponseBody
    public ResponseData del(String[] ids) {
      return bpmCrmRemittanceService.deletebpmCrmRemittanceByIds(ids)? success(): error("删除失败!");
    }    
}
